package ch.fhnw.qualiscan.application.elements;

import java.util.ArrayList;
import java.util.Set;

import ch.fhnw.qualiscan.application.models.QuestionModel;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.QuestionText;
import ch.fhnw.qualiscan.core.helpers.Filter;
import ch.fhnw.qualiscan.core.helpers.UserSessionHelper;

import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class EvaluationQuestion extends VerticalLayout {
	
	Question question;
	
	UserSessionHelper sessionHelper;
	
	Filter filter;
	
	int counter;
	
	ArrayList<EvaluationQuestionForm> questionItems = new ArrayList<EvaluationQuestionForm>();
	
	public EvaluationQuestion(Question question, Filter filter, int counter) {
		this.filter = filter;
		this.counter = counter;
		
		this.question = question;
		sessionHelper = new UserSessionHelper();
		
		buildElement();
	}
	
	public void buildElement() {
		QuestionModel questionModel = new QuestionModel();
		QuestionText questionText = questionModel.getQuestionText(sessionHelper.getUserSession().getLanguage(), question);
		
		// has subquestions?
		Set<Question> subquestions = question.getSubquestions();
		
		// no subquestions
		if(subquestions.isEmpty()) {
			EvaluationQuestionForm newItem = new EvaluationQuestionForm(counter, -1, questionText.getText(), false, filter, question.getId());
			questionItems.add(newItem);
			addComponent(newItem);
		}
		
		// with subquestions
		else {
			addComponent(new QuestionHeader(counter, questionText.getText(), true));
		
			int i = 0;
			for(Question subquestion : subquestions) {
				i++;
				QuestionText subQuestionText = questionModel.getQuestionText(sessionHelper.getUserSession().getLanguage(), subquestion);
				EvaluationQuestionForm newItem = new EvaluationQuestionForm(counter, i, subQuestionText.getText(), true, filter, subquestion.getId());
				addComponent(newItem);
				questionItems.add(newItem);
			}
		}
	}
		
	
	public void update(Filter filter) {
		this.filter = filter;

		for(EvaluationQuestionForm Item : questionItems) {
			Item.update(filter);
		}
		
	}
	
}
